package cn.lingyangwl.agile.food.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.lingyangwl.agile.food.core.assembly.GoodsSkuAssembly;
import cn.lingyangwl.agile.food.core.manager.GoodsSkuManager;
import cn.lingyangwl.agile.food.core.mapper.SkuStockMapper;
import cn.lingyangwl.agile.food.model.entity.GoodsSku;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuPageReq;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuReq;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuResp;
import cn.lingyangwl.agile.food.core.service.GoodsSkuService;
import cn.lingyangwl.framework.mybatis.plus.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 商品sku
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class GoodsSkuServiceImpl extends ServiceImpl<SkuStockMapper, GoodsSku>
    implements IService<GoodsSku>, GoodsSkuService {

    @Resource
    private GoodsSkuManager skuManager;
    @Resource
    private GoodsSkuAssembly skuAssembly;
    
    @Override
    public IPage<SkuResp> pageSku(SkuPageReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<GoodsSku> lqw = GoodsSku.lqw()
            .eq(StringUtils.isNotEmpty(req.getSkuCode()), GoodsSku::getSkuCode, req.getSkuCode())
            .ge(Objects.nonNull(beginTime), GoodsSku::getCreatedAt, beginTime)
            .le(Objects.nonNull(endTime), GoodsSku::getCreatedAt, endTime)
            .orderByDesc(GoodsSku::getCreatedAt);
        IPage<GoodsSku> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, skuAssembly::toSkuResp);
    }

    @Override
    public SkuResp getSku(Long id) {
        GoodsSku entity = this.getById(id);
    	return skuAssembly.toSkuResp(entity);
    }

    @Override
    public boolean saveSku(SkuReq req) {
        GoodsSku entity = skuAssembly.toSku(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateSku(SkuReq req) {
        GoodsSku entity = skuAssembly.toSku(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteSku(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteSku(List<Long> ids) {
    	return this.removeByIds(ids);
    }

    @Override
    public List<GoodsSku> listSkuByGoodsIds(List<Long> goodsIds) {
        if (CollUtil.isEmpty(goodsIds)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<GoodsSku> lqw = GoodsSku.lqw().in(GoodsSku::getGoodsId, goodsIds);
        return this.list(lqw);
    }
}
